package com.ckd.flyingtrip.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.adapter.UserInfoAddressAdapter;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.javabean.UserInfoAddressBean;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserInfoAddressActivity extends BaseActivity {

    @BindView(R.id.activity_userinfo_address_recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.toolber_color_title)
    TextView toolberTitle;
    @BindView(R.id.activity_userinfo_address_no)
    TextView activityUserinfoAddressNo;
    @BindView(R.id.toolber_color_right)
    TextView right;

    private UserInfoAddressAdapter adapter;
    private List<UserInfoAddressBean.DataBean> dataBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo_address);
        ButterKnife.bind(this);

        initView();
        initListener();

    }

    private void initListener() {
        adapter.setOnItemClickListener(new UserInfoAddressAdapter.OnItemClickListener() {
            @Override
            public void onModifyItemClick(View view, int position) {
                Intent intent = new Intent(UserInfoAddressActivity.this, UserInfoAddressAddActivity.class);
                intent.putExtra("title", "修改地址");
                intent.putExtra("data", (Serializable) dataBeans.get(position));
                startActivity(intent);
            }

            @Override
            public void onDeleteItemClick(View view, int position) {
                initDelete(dataBeans.get(position).getAddressId());
            }
        });
    }

    private void initDelete(String addressId) {
        String url = Constants.deleteaddress;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.e("删除地址", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    if (code.equals("1")) {
                        Toast.makeText(UserInfoAddressActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
                        initData();
                    } else {
                        Log.i("站好", "账号");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dismisProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("sss", volleyError + "");
                dismisProgressDialog();
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("addressId", addressId);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        String url = Constants.getaddresslist;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                //s为请求返回的字符串数据
                Log.e("地址列表", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    if (code.equals("1")) {
                        UserInfoAddressBean bean = new Gson().fromJson(s, UserInfoAddressBean.class);
                        if (bean.getData().size()==0){
                            recyclerView.setVisibility(View.GONE);
                            activityUserinfoAddressNo.setVisibility(View.VISIBLE);
                        }else {
                            activityUserinfoAddressNo.setVisibility(View.GONE);
                            recyclerView.setVisibility(View.VISIBLE);
                        }
                        dataBeans.clear();
                        dataBeans.addAll(bean.getData());
                        if (adapter != null)
                            adapter.notifyDataSetChanged();
                    } else {
                        Log.i("站好", "账号");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dismisProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("sss", volleyError + "");
                dismisProgressDialog();
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    private void initView() {
        right.setText("添加新地址");
        right.setVisibility(View.VISIBLE);
        toolberTitle.setText("收货地址");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new UserInfoAddressAdapter(this, dataBeans);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @OnClick({R.id.toolber_color_back, R.id.toolber_color_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolber_color_back:
                finish();
                break;
            case R.id.toolber_color_right:
                Intent intent = new Intent(this, UserInfoAddressAddActivity.class);
                intent.putExtra("title", "添加地址");
                startActivity(intent);
                break;
        }
    }

}
